﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PlanckTime.Entity;
using Microsoft.ManagementConsole.SnapIns.Integration;

namespace Microsoft.ManagementConsole.SnapIns.Controls
{
    public partial class PlanckTimeTaskDetailControl : UserControl, IFormViewControl
    {
        public EventLog[] EventLogs { get; set; }
        public Task CurrentTask { get; set; }
        public TaskData[] TaskData { get; set; }

        public PlanckTimeTaskDetailControl()
        {
            InitializeComponent();

            this.Dock = DockStyle.Fill;
        }

        public void Initialize(FormView view)
        {
            CurrentTask = view.ViewDescriptionTag as Task;

            if (CurrentTask != null)
            {
                lblTaskNameValue.Text = CurrentTask.Name;
                lblTaskStatuValue.Text = CurrentTask.Status;
                lblAssignedToValue.Text = GetUserDisplayName(CurrentTask.AssignedToId);
                lblDateCreatedValue.Text = CurrentTask.DateCreated.ToString();
                lblLastUpdatedValue.Text = CurrentTask.DateLastUpdated.ToString();
                lblLastUpdatedByValue.Text = GetUserDisplayName(CurrentTask.LastUpdatedBy);

                InitializeEventLog(CurrentTask.Id);
                InitializeTaskData(CurrentTask.Id);

            }
        }

        private string GetUserDisplayName(Guid userId)
        {
            if (userId != Guid.Empty)
            {
                return PlanckTimeServices.Instance.GetUserDisplayName(userId);
            }

            return "--------------";
        }

        private void InitializeEventLog(Guid taskId)
        {
            if (taskId != Guid.Empty)
            {
                var basicEntityCollection = PlanckTimeServices.Instance.GetEventLog(taskId);

                if (basicEntityCollection != null)
                {
                    var eventLogs = basicEntityCollection.ConvertTo<EventLog>();

                    if (eventLogs != null)
                    {
                        this.EventLogs = eventLogs;

                        foreach (var item in eventLogs)
                        {
                            dtgEventLog.Rows.Add(item.Id, item.Title, item.Description);
                        }
                    }
                }
            }

        }

        private void InitializeTaskData(Guid taskId)
        {
            if (taskId != Guid.Empty)
            {
                var basicEntityCollection = PlanckTimeServices.Instance.GetTaskData(taskId);

                if (basicEntityCollection != null)
                {
                    var taskData = basicEntityCollection.ConvertTo<TaskData>();

                    if (taskData != null)
                    {
                        this.TaskData = taskData;

                        foreach (var item in taskData)
                        {
                            dtgEventLog.Rows.Add(item.Id, item.Title);
                        }
                    }
                }
            }

        }

        private void dtgEventLog_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.EventLogs != null)
            {
                try
                {
                    var eventLog = this.EventLogs.Where(q => q.Id == Guid.Parse(dtgEventLog[0, e.RowIndex].Value.ToString())).ToList().First();

                    EventLogDetailForm detailForm = new EventLogDetailForm(eventLog, CurrentTask);
                    detailForm.Show();
                }
                finally { }
            }
        }

        private void dtg_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

    }
}
